package com.isczy.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.eduservice.entity.EduTeacher;
import com.isczy.eduservice.mapper.EduTeacherMapper;
import com.isczy.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author CZY
 * @since 2020-12-25
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    /***
     * ***************************************
     * 功能描述:查询热门教师，并放入缓存
     * @param
     * @return {@link List< EduTeacher>}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-13 16:45】
     * @author modify:
     */
    @Override
    @Cacheable(value = "hot",key = "'teacher'")
    public List<EduTeacher> getHotTeacherList() {
        //查询前4条名师
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        return baseMapper.selectList(wrapperTeacher);
    }

    /***
     * ***************************************
     * 功能描述:分页查询讲师的方法
     * @param pageParam
     * @return {@link Map< String, Object>}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:04】
     * @author modify:
     */
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装到pageTeacher对象
        baseMapper.selectPage(pageParam,wrapper);

        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }
}
